package com.zm.spring.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component
@Aspect
public class MyAspect {

    //切点方法
    @Pointcut("execution(* com.zm.spring.demo..*.find*(..))")
    public void pointcut1(){

    }

    @Before("pointcut1()")
    //接口注入
    public void before(JoinPoint joinPoint){
        System.out.println("========前置增强=======");
        System.out.println("joinPoint.getSignature() = " + joinPoint.getSignature());
        System.out.println("Arrays.toString(joinPoint.getArgs()) = " + Arrays.toString(joinPoint.getArgs()));
        System.out.println("joinPoint.getTarget() = " + joinPoint.getTarget());
    }

    @After("pointcut1()")
    public void after(JoinPoint joinPoint){
        System.out.println("========后置增强=======");
        System.out.println("joinPoint.getSignature() = " + joinPoint.getSignature());
        System.out.println("Arrays.toString(joinPoint.getArgs()) = " + Arrays.toString(joinPoint.getArgs()));
        System.out.println("joinPoint.getTarget() = " + joinPoint.getTarget());
    }
//    "execution(* com.zm.spring.demo.web.*.find*(..))"
    @AfterReturning("pointcut1()")
    public void afterReturnning(JoinPoint joinPoint){
        System.out.println("========返回置增强=======");
        System.out.println("joinPoint.getSignature() = " + joinPoint.getSignature());
        System.out.println("Arrays.toString(joinPoint.getArgs()) = " + Arrays.toString(joinPoint.getArgs()));
        System.out.println("joinPoint.getTarget() = " + joinPoint.getTarget());
    }

    @AfterThrowing("pointcut1()")
    public void afterThrowing(JoinPoint joinPoint){
        System.out.println("========异常增强=======");
        System.out.println("joinPoint.getSignature() = " + joinPoint.getSignature());
        System.out.println("Arrays.toString(joinPoint.getArgs()) = " + Arrays.toString(joinPoint.getArgs()));
        System.out.println("joinPoint.getTarget() = " + joinPoint.getTarget());
    }

    /**
     * try{
     *          前置增强
     *     业务方法()
     *          返回增强
     * }catch(Exception e){
     *          异常增强
     * }finally{
     *          后置增强
     * }
     *
     */

    @Around("pointcut1()")
    public Object around(ProceedingJoinPoint proceedingJoinPoint){
        //ProceedingJoinPoint 提供执行业务方法的方法
        //业务方法由增强方法执行
        //增强方法的返回值就是业务方法的返回值
        System.out.println("AAAAAAAAAAAAAAAAAA");
        long time1 = System.currentTimeMillis();
        long time2 = 0;
        try{
            System.out.println("AAAAAAAAAAAAAAAAAA");
            Object ret = proceedingJoinPoint.proceed();
            time2 = System.currentTimeMillis();
            System.out.println("AAAAAAAAAAAAAAAAAA");
            return ret;
        }catch (Throwable e){
            time2 = System.currentTimeMillis();
            throw new RuntimeException(e);
        }finally {
            System.out.println("环绕增强，共耗时：" + (time2 - time1));
        }
    }


}
